home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / MoofWars / MoofEncoder / PICTMask.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  6.9 KB  |  219 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        PICTMask.cp
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Timothy Carroll    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/1/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 2/23/98        Timothy Carroll    When we created the PICT, we weren't setting
  21.                                             the clip rectangle.  The default clipping region
  22.                                             would cause problems if we ever scaled the PICT 
  23.                                             when we draw it.
  24.                 2/24/97        Timothy Carroll    Now explicitly include main.h
  25.                 8/15/96        Timothy Carroll Initial Release
  26.                 
  27.                 
  28.                 
  29.  
  30. */
  31. #include <Resources.h>
  32. #include "Main.h"
  33. #include "Color Search Procs.h"
  34. #include "PICTMask.h"
  35.  
  36. OSStatus GeneratePICTMasks (short inputFileResNum, short outputFileResNum)
  37. {
  38.     OSStatus        theErr;
  39.  
  40.     UInt16            numPicts, loop;
  41.     
  42.     // we pass these to GetResInfo so that we can get the actual resource ID.
  43.     SInt16            resID;
  44.     ResType            resType;
  45.     Str255            resName;
  46.  
  47.     // Holds the last value on the resource chain since we change the top resource a lot
  48.     SInt16            saveResNum;
  49.     
  50.     // The current picture being worked on, along with its GWorld and Pixmap
  51.     PicHandle        thePicture = NULL;
  52.     GWorldPtr        pictGWorld = NULL;
  53.     PixMapHandle    pictPixMap = NULL;
  54.     OpenCPicParams    theParams; // used to build the output picture
  55.     Rect            pictRect, drawRect, sourceRect;
  56.     
  57.     // Saves port info while we draw into the GWorld
  58.     CGrafPtr        savePort;
  59.     GDHandle         saveDevice;
  60.     
  61.     SInt16            offset;
  62.     
  63.     Boolean            pictIsResource = false; // Determines if we call ReleaseResource or DisposeHandle
  64.     
  65.     // Save off the current resource chain so that we can restore it later
  66.     saveResNum = CurResFile();
  67.  
  68.     UseResFile (inputFileResNum);
  69.  
  70.  
  71.  
  72.     // First thing is to copy the color table resource to the output.
  73.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  74.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  75.     
  76.     // get the color table
  77.     gAppColorTable = GetCTable( kAppColorTableResID );
  78.     FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
  79.  
  80.     // determine the number of icon resources
  81.     numPicts = Count1Resources( 'PICT' );
  82.     
  83.     // get each one,
  84.     for( loop = 1; loop <= numPicts; loop++ )
  85.     {
  86.     // *********************************************************************
  87.     // Load the PICT and get the resource information
  88.     // *********************************************************************
  89.         UseResFile (inputFileResNum);
  90.         pictIsResource = true;
  91.         thePicture = (PicHandle) Get1IndResource ('PICT', loop);
  92.         theErr = ResError();
  93.         FAIL_NIL (thePicture, "\pFailed to load the picture")
  94.         FAIL_OSERR (theErr, "\pFailed to load the picture")
  95.         
  96.         GetResInfo( (Handle) thePicture, &resID, &resType, resName );
  97.         theErr = ResError();
  98.         FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  99.         
  100.     // *********************************************************************
  101.     // Create a GWorld, erase it, and draw our pictures and masks into it
  102.     // We'll have three images across, left to right, in the GWorld
  103.     // *********************************************************************
  104.         pictRect = (**thePicture).picFrame;
  105.         OffsetRect (&pictRect, -pictRect.left, -pictRect.top);
  106.         drawRect = pictRect;
  107.         pictRect.right *= 3;
  108.         offset = drawRect.right;
  109.     
  110.         theErr = NewGWorld(&pictGWorld, kPreferredDepth, &pictRect, gAppColorTable, NULL, 0);
  111.         FAIL_OSERR (theErr, "\pFailed to allocate the GWorld")
  112.         FAIL_NIL (pictGWorld, "\pFailed to allocate the GWorld")
  113.     
  114.         pictPixMap  = GetGWorldPixMap(pictGWorld);
  115.         FAIL_NIL (pictPixMap, "\pCouldn't get the pixmap")
  116.         FAIL_FALSE ( LockPixels(pictPixMap), "\pCouldn't lock the pixmap")
  117.         
  118.         GetGWorld (&savePort, &saveDevice);
  119.         SetGWorld (pictGWorld, NULL);
  120.     
  121.         EraseRect (&pictRect);
  122.         DrawPicture (thePicture, &drawRect);
  123.         
  124.         sourceRect = drawRect;
  125.         
  126.         drawRect.left +=offset;
  127.         drawRect.right +=offset;
  128.         
  129.         // The top left corner of the picture is always assumed to be the Mask color!
  130.         // We set our mask color and insert our search proc, then we copy the mask.
  131.         
  132.         GetCPixel (0,0, &gMaskColor);
  133.         AddSearch (MaskSearchProcUPP);
  134.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap,
  135.                 &sourceRect, &drawRect, srcCopy, NULL);
  136.         DelSearch (MaskSearchProcUPP);
  137.         
  138.         sourceRect = drawRect;
  139.         drawRect.left+=offset;
  140.         drawRect.right+=offset;
  141.         
  142.         // The second is just a copy of the first.  No search is necessary
  143.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap,
  144.         &sourceRect, &drawRect, srcCopy, NULL);
  145.  
  146.     // *********************************************************************
  147.     // We're done with the old picture, so we can release it and build the
  148.     // new picture to add to the output file.
  149.     // *********************************************************************
  150.         ReleaseResource ((Handle) thePicture);
  151.         thePicture = NULL;
  152.     
  153.         pictIsResource = false;
  154.         
  155.         theParams.srcRect = pictRect;
  156.         theParams.hRes = 0x00480000;
  157.         theParams.vRes = 0x00480000;
  158.         theParams.version = -2;
  159.         theParams.reserved1 = 0;
  160.         theParams.reserved2 = 0;
  161.     
  162.         thePicture = OpenCPicture (&theParams);
  163.         ClipRect (&pictRect);
  164.         CopyBits ((BitMap *) *pictPixMap, (BitMap *) *pictPixMap, &pictRect, &pictRect, srcCopy, NULL);
  165.         ClosePicture();
  166.         theErr = QDError();
  167.         FAIL_NIL (thePicture, "\pFailed to create the new picture")
  168.         FAIL_OSERR (theErr, "\pFailed to create the new picture")
  169.         
  170.         SetGWorld (savePort, saveDevice);
  171.     
  172.     // *********************************************************************
  173.     // Add the new PICT to the resource file
  174.     // *********************************************************************
  175.     // We're done with the GWorld
  176.         DisposeGWorld (pictGWorld);
  177.         pictGWorld = NULL;
  178.     
  179.         UseResFile (outputFileResNum);
  180.         AddResource( (Handle) thePicture, 'PICT', resID, resName );
  181.         theErr = ResError();
  182.         FAIL_OSERR (theErr,"\pFailed to add the resource to the output file")
  183.     
  184.         pictIsResource = true;
  185.     
  186.         WriteResource( (Handle) thePicture );
  187.         theErr = ResError();
  188.         FAIL_OSERR (theErr,"\pFailed to write the resource to the output file")
  189.     
  190.         ReleaseResource( (Handle) thePicture );
  191.         thePicture = NULL;
  192.     }
  193.     
  194.     goto cleanup;
  195.     
  196. error:
  197.     
  198.     if (theErr == noErr)
  199.         theErr = paramErr;
  200.         
  201. cleanup:
  202.     
  203.     UseResFile (saveResNum);
  204.     
  205.     if (pictGWorld != NULL)
  206.         DisposeGWorld (pictGWorld);
  207.     
  208.     if (thePicture != NULL)
  209.         if (pictIsResource)
  210.             ReleaseResource ((Handle) thePicture);
  211.         else
  212.             DisposeHandle ((Handle) thePicture);
  213.     
  214.     if (gAppColorTable != NULL)
  215.         DisposeCTable (gAppColorTable);
  216.     gAppColorTable = NULL;
  217.     return theErr;
  218. }
  219.